#include <time.h>
#include <stdlib.h>
#include <peekpoke.h>

#include "globalInput.h"
#include "Timer.h"
#include "Screen.h"

unsigned char months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

unsigned char fromBCD(unsigned char BCD)
{
	unsigned char hi = (BCD & 0x70) >> 4;
	unsigned char lo = (BCD & 0x0F);
	unsigned char result = hi*10 + lo;

	return result;
}

void printTime(const unsigned char* message, ts_t* time)
{
	waitForEnterEscf("%s: %02u:%02u:%02u.%03u - %llu", message, time->Hour, time->Minute, time->Second, time->Milisecond, time->Time);
}

ts_t* getTime()
{
	ts_t* now;
	unsigned long nowTime;
	unsigned char hourBCD = PEEK(0xDC0B);
	unsigned char minBCD = PEEK(0xDC0A);
	unsigned char secBCD = PEEK(0xDC09);
	unsigned char tenthsBCD = PEEK(0xDC08);
	unsigned char afternoon = (hourBCD & 0x80);

	unsigned char hour = fromBCD(hourBCD & 0x7F);
	unsigned char min = fromBCD(minBCD);
	unsigned char sec = fromBCD(secBCD);
	unsigned char tenths = fromBCD(tenthsBCD);

	if(afternoon > 0)
	{
		hour += 12;
	}

	--hour;

	now = (ts_t*)malloc(sizeof(ts_t));

	now->Year = 1971;
	now->Month = 01;
	now->Day = 01;

	now->Hour = hour;
	now->Minute = min;
	now->Second = sec;
	now->Milisecond = tenths * 100u;

	nowTime = (unsigned long)(((unsigned int)hour * 3600u) + ((unsigned int)min * 60u) + (unsigned int)sec) * 1000ul + now->Milisecond;
	now->Time = nowTime;

	//waitForEnterEscf("%2u:%2u:%2u.%u", hourBCD, minBCD, secBCD, tenthsBCD);
	//waitForEnterEscf("%2u:%2u:%2u.%u - %u", hour, min, sec, tenths, nowTime);

	//nowTime = (unsigned int)time(NULL);

	//now->Time = nowTime;

	//now->Year = 2011;
	//now->Day = 25;
	//now->Month = 11;
	//
	//now->Hour = nowTime / 3600;
	//now->Minute = (nowTime - (nowTime / 3600))/60;
	//now->Second = nowTime - (now->Hour * 3600 * now->Minute * 60);
	//now->Milisecond = 0;



	return now;
}

ts_t* getElapsedTime(ts_t* startTime, ts_t* endDate)
{
	ts_t* result;

	//printTime("startTime", startTime);
	//printTime("endDate", endDate);

	result = (ts_t*)malloc(sizeof(ts_t));

	result->Time = endDate->Time - startTime->Time;

	result->Year = endDate->Year - startTime->Year;
	result->Month = (result->Year*12 + endDate->Month) - startTime->Month;
	
	if(result->Year > 0)
	{
		result->Day = (result->Year*365);
	}
	else
	{
		if(result->Month > 0)
		{

		}
		else
		{
			result->Day = (unsigned char)((signed char)(endDate->Day) - (signed char)(startTime->Day));
		}
	}

	if(result->Day > 0)
	{
		if(endDate->Hour < startTime->Hour)
		{
			--result->Day; 
			result->Hour = (24 - startTime->Hour) + endDate->Hour;
		}
		else
		{
			result->Hour = endDate->Hour - startTime->Hour;
		}
	}
	else
	{
		result->Hour = endDate->Hour - startTime->Hour;
	}

	if(result->Hour > 0)
	{
		if(endDate->Minute < startTime->Minute)
		{
			--result->Hour; 
			result->Minute = (60 - startTime->Minute) + endDate->Minute;
		}
		else
		{
			result->Minute = endDate->Minute - startTime->Minute;
		}
	}
	else
	{
		result->Minute = endDate->Minute - startTime->Minute;
	}

	if(result->Minute > 0)
	{
		if(endDate->Second < startTime->Second)
		{
			--result->Minute; 
			result->Second = (60 - startTime->Second) + endDate->Second;
		}
		else
		{
			result->Second = endDate->Second - startTime->Second;
		}
	}
	else
	{
		result->Second = endDate->Second - startTime->Second;
	}

	if(result->Second > 0)
	{
		if(endDate->Milisecond < startTime->Milisecond) 
		{
			--result->Second;
			result->Milisecond = (1000u - startTime->Milisecond) + endDate->Milisecond;
		}
		else
		{
			result->Milisecond = endDate->Milisecond - startTime->Milisecond;
		}
	}
	else
	{
		result->Milisecond = endDate->Milisecond - startTime->Milisecond;
	}

	//printTime("result", result);

	return result;
}

void setClock(unsigned char hour, unsigned char minute, unsigned char second, unsigned char tenths)
{
	POKE(0xDC0B, hour);
	POKE(0xDC0A, minute);
	POKE(0xDC09, second);
	POKE(0xDC08, tenths);
}